using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using Scopa.SaveData;

namespace Scopa.Menu
{
    using Locale = ScopaLocale;

  public interface IStartMenu
  {
    event MenuEvent OnPlayClicked;
    event MenuEvent OnExitClicked;
    event MenuEvent OnContinue;
  }
  
  public class StartMenu : BaseMenu, IStartMenu
  {
    Button playBtn, exitBtn, continueBtn;
    SpriteBatch spriteBatch;    

    public StartMenu(Game game, MenuDepths depth)
      : base(game, depth)
    {
      spriteBatch = game.GetScopaContext().SpriteBatch;
      bounds = game.GraphicsDevice.Viewport.Bounds;
      game.Services.AddService(typeof(IStartMenu), this);
    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(IStartMenu));
      base.Dispose(disposing);
    }

    public override void Initialize()
    {    
      // Play button
      playBtn = new Button(Game, @"Images//Menu//button2", new Rectangle(Game.GraphicsDevice.Viewport.Bounds.X + 625, 400, 120, 65), Color.Blue, "small_font", Locale.Strings.NewGame.Replace("\\n", "\n"), Color.White);
      AddButton(playBtn, () =>
      {
        playBtn.touched = true;
      });

      // Exit button
      exitBtn = new Button(Game, @"Images//Menu//button2", new Rectangle(Game.GraphicsDevice.Viewport.Bounds.X + 70, 400, 120, 65), Color.Red, "large_font", Locale.Strings.Exit, Color.White);
      AddButton(exitBtn, () =>
      {
        exitBtn.touched = true;        
      });

      // Cuntinue button
      continueBtn = new Button(Game, @"Images//Menu//Button2", new Rectangle(410, 400, 120, 65), Color.Green, "small_font", Locale.Strings.ContinueGame.Replace("\\n", "\n"), Color.White);

      if (Game.GetScopaContext().currentConfig.hasSavedGame)
      {
        AddButton(continueBtn, () =>
         {
           continueBtn.touched = true;
         });
      }

      base.Initialize();
    }


    int counter = 4;
    public override void Update(GameTime gameTime)
    {
      if (playBtn.touched)
      {
        counter--;
        if (counter < 0)
        {
          if ((OnPlayClicked != null))
            OnPlayClicked();
        }
      }

      if (exitBtn.touched)
      {
        counter--;
        if (counter < 0)
        {
          if ((OnExitClicked != null))
            OnExitClicked();
        }
      }

      if (continueBtn.touched)
      {
        counter--;
        if (counter < 0)
        {
          if ((OnContinue != null))
            OnContinue();
        }
      }
      
      // Input: back button pressed
      if (((Game1)Game).IsBackButtonPressed())
      {
        if (OnExitClicked != null)
          OnExitClicked();
      }

      base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
      base.Draw(gameTime);
    }

    public event MenuEvent OnPlayClicked;
    public event MenuEvent OnExitClicked;
    public event MenuEvent OnContinue;
 
  }
}
